//
//  ByUIDataImportHistoryVC.m
//  laserdevice_ios
//
//  Created by pro on 16/12/15.
//  Copyright © 2016年 com.itboye. All rights reserved.
//

#import "ByUIDataImportHistoryVC.h"

#import "ByUIDataImportHistoryCell.h"

@interface ByUIDataImportHistoryVC () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collection;

@property (nonatomic, strong) NSArray *items;

@end

@implementation ByUIDataImportHistoryVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = STRING(@"lishishujudaoru");
    
    [self addLeftBackBarButtonItem];
    
    self.items = [ByFileManager allHistorys];
    
    [self createCollectionView];
}

- (void)createCollectionView {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    CGFloat width = (SCREEN_WIDTH) / 2;
    CGFloat height = 100;
    layout.itemSize = CGSizeMake(width, height);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) collectionViewLayout:layout];
    self.collection.backgroundColor = TABLE_COLOR;
    self.collection.delegate = self;
    self.collection.dataSource = self;
    [self.view addSubview:self.collection];
    [self.collection registerClass:[ByUIDataImportHistoryCell class] forCellWithReuseIdentifier:@"ByUIDataImportHistoryCell"];
}

#pragma mark - UICollectionViewDatasource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.items.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ByUIDataImportHistoryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ByUIDataImportHistoryCell" forIndexPath:indexPath];
    cell.jsonDic = self.items[indexPath.item];
    cell.delectAction = ^ {
        
        [UIAlertView showWithTitle:STRING(@"shanchu excel") message:STRING(@"shanchu excel-jinggao") cancelButtonTitle:STRING(@"fou") otherButtonTitles:@[STRING(@"shi")] tapBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {
            
            if (buttonIndex == 1) {
                NSString *sceneName = self.items[indexPath.item][@"name"];
                [ByFileManager deleteHistoryWithName:sceneName
                                             success:^{
                                                 NSMutableArray *temp = [[NSMutableArray alloc] initWithArray:self.items];
                                                 [temp removeObjectAtIndex:indexPath.row];
                                                 self.items = temp.copy;
                                                 [self.collection reloadData];
                                             }
                                             failure:^(NSError *error) {
                                               
                                                 NSLog(@"删除失败:%@", error.domain);
                                             }];
                
                
            }
        }];
    };
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary *jsonDic = self.items[indexPath.row];
    
    [UIAlertView showWithTitle:STRING(@"bianji excel") message:STRING(@"bianji excel-jinggao") cancelButtonTitle:STRING(@"woyaoxianbaocun") otherButtonTitles:@[STRING(@"bianji excel")] tapBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {
        
        if (buttonIndex == 1) {
            if (self.backAction) {
                self.backAction(jsonDic);
                [self dismissViewControllerAnimated:YES completion:nil];
            }
        }
    }];
    
    
}
@end
